package com.example.hzq.zqb.activity;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.adapter.CommentAdapter;
import com.example.hzq.zqb.bean.CommentBean;
import com.example.hzq.zqb.model.Model;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import static com.example.hzq.zqb.adapter.CampusshopAdapter.goodid;

/**
 * Created by hzq on 2017/7/10.
 */

public class CommentActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageButton back;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager manager;
    private int page,lastVisibleItemPosition;
    private List<CommentBean> list=null;
    private CommentAdapter commentAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment);
        page=1;
        back=(ImageButton)findViewById(R.id.back2);
        recyclerView=(RecyclerView)findViewById(R.id.commentrecycle);
        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        commentAdapter =new CommentAdapter(CommentActivity.this);
        back.setOnClickListener(this);
        commentAdapter.setList(list);
        Model model=new Model();
        Call<List<CommentBean>> call=model.getCommentList(page,goodid);
        call.enqueue(new Callback<List<CommentBean>>() {
            @Override
            public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    list=response.body();
                    commentAdapter.setList(list);
                }else
                    Toast.makeText(CommentActivity.this,"获取资源失败", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                Toast.makeText(CommentActivity.this,"404.commentactivity", Toast.LENGTH_SHORT).show();
            }
        });
       recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
           @Override
           public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
               super.onScrollStateChanged(recyclerView, newState);
               if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                   page+=1;
                   Model model=new Model();
                   Call<List<CommentBean>> call=model.getCommentList(page,goodid);
                   call.enqueue(new Callback<List<CommentBean>>() {
                       @Override
                       public void onResponse(Call<List<CommentBean>> call, Response<List<CommentBean>> response) {
                           if(response.isSuccessful()&&response.body()!=null)
                           {
                               list.addAll(response.body());
                               commentAdapter.setList(list);
                           }else{
                               Toast.makeText(CommentActivity.this,"获取资源失败", Toast.LENGTH_SHORT).show();
                           }
                       }

                       @Override
                       public void onFailure(Call<List<CommentBean>> call, Throwable t) {
                           Toast.makeText(CommentActivity.this,"请检查网络连接",Toast.LENGTH_SHORT).show();
                       }
                   });
               }
           }

           @Override
           public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
               super.onScrolled(recyclerView, dx, dy);
               lastVisibleItemPosition=list.size()-1;
           }
       });
        recyclerView.setAdapter(commentAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back2:
                finish();
                break;
            default:break;
        }
    }
}
